home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch11 / Hermite.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-12  |  9KB  |  274 lines

  1. VERSION 5.00
  2. Begin VB.Form frmHermite 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Hermite"
  5.    ClientHeight    =   5685
  6.    ClientLeft      =   1650
  7.    ClientTop       =   360
  8.    ClientWidth     =   4830
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   379
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Begin VB.CheckBox chkControlPoints 
  15.       Caption         =   "Draw Control Points"
  16.       Height          =   255
  17.       Left            =   1440
  18.       TabIndex        =   12
  19.       Top             =   60
  20.       Value           =   1  'Checked
  21.       Width           =   1815
  22.    End
  23.    Begin VB.CommandButton cmdGo 
  24.       Caption         =   "Go"
  25.       Default         =   -1  'True
  26.       Height          =   375
  27.       Left            =   4320
  28.       TabIndex        =   11
  29.       Top             =   0
  30.       Width           =   495
  31.    End
  32.    Begin VB.TextBox txtVy2 
  33.       Height          =   285
  34.       Left            =   4200
  35.       TabIndex        =   9
  36.       Text            =   "500"
  37.       Top             =   480
  38.       Width           =   615
  39.    End
  40.    Begin VB.TextBox txtVx2 
  41.       Height          =   285
  42.       Left            =   3120
  43.       TabIndex        =   7
  44.       Text            =   "-500"
  45.       Top             =   480
  46.       Width           =   615
  47.    End
  48.    Begin VB.TextBox txtVy1 
  49.       Height          =   285
  50.       Left            =   1440
  51.       TabIndex        =   5
  52.       Text            =   "-500"
  53.       Top             =   480
  54.       Width           =   615
  55.    End
  56.    Begin VB.TextBox txtVx1 
  57.       Height          =   285
  58.       Left            =   360
  59.       TabIndex        =   3
  60.       Text            =   "-500"
  61.       Top             =   480
  62.       Width           =   615
  63.    End
  64.    Begin VB.TextBox txtDt 
  65.       Height          =   285
  66.       Left            =   240
  67.       TabIndex        =   2
  68.       Text            =   "0.01"
  69.       Top             =   45
  70.       Width           =   615
  71.    End
  72.    Begin VB.PictureBox picCanvas 
  73.       AutoRedraw      =   -1  'True
  74.       Height          =   4815
  75.       Left            =   0
  76.       ScaleHeight     =   317
  77.       ScaleMode       =   3  'Pixel
  78.       ScaleWidth      =   317
  79.       TabIndex        =   0
  80.       Top             =   840
  81.       Width           =   4815
  82.    End
  83.    Begin VB.Label Label1 
  84.       Caption         =   "Vy2"
  85.       Height          =   255
  86.       Index           =   4
  87.       Left            =   3840
  88.       TabIndex        =   10
  89.       Top             =   510
  90.       Width           =   375
  91.    End
  92.    Begin VB.Label Label1 
  93.       Caption         =   "Vx2"
  94.       Height          =   255
  95.       Index           =   3
  96.       Left            =   2760
  97.       TabIndex        =   8
  98.       Top             =   510
  99.       Width           =   375
  100.    End
  101.    Begin VB.Label Label1 
  102.       Caption         =   "Vy1"
  103.       Height          =   255
  104.       Index           =   2
  105.       Left            =   1080
  106.       TabIndex        =   6
  107.       Top             =   510
  108.       Width           =   375
  109.    End
  110.    Begin VB.Label Label1 
  111.       Caption         =   "Vx1"
  112.       Height          =   255
  113.       Index           =   0
  114.       Left            =   0
  115.       TabIndex        =   4
  116.       Top             =   510
  117.       Width           =   375
  118.    End
  119.    Begin VB.Label Label1 
  120.       Caption         =   "dt"
  121.       Height          =   255
  122.       Index           =   1
  123.       Left            =   0
  124.       TabIndex        =   1
  125.       Top             =   60
  126.       Width           =   255
  127.    End
  128. Attribute VB_Name = "frmHermite"
  129. Attribute VB_GlobalNameSpace = False
  130. Attribute VB_Creatable = False
  131. Attribute VB_PredeclaredId = True
  132. Attribute VB_Exposed = False
  133. Option Explicit
  134. Private Const GAP = 2
  135. ' The endpoints.
  136. Private Const NumPts = 2
  137. Private PtX(1 To NumPts) As Single
  138. Private PtY(1 To NumPts) As Single
  139. ' The index of the point being dragged.
  140. Private Dragging As Integer
  141. ' The hermite curve parameters.
  142. Private Ax As Single
  143. Private Bx As Single
  144. Private Cx As Single
  145. Private Dx As Single
  146. Private Ay As Single
  147. Private By As Single
  148. Private Cy As Single
  149. Private Dy As Single
  150. ' Draw the curve on the indicated picture box.
  151. Private Sub DrawCurve(ByVal pic As PictureBox, ByVal start_t As Single, ByVal stop_t As Single, ByVal dt As Single)
  152. Dim t As Single
  153.     pic.Cls
  154.     pic.CurrentX = X(start_t)
  155.     pic.CurrentY = Y(start_t)
  156.     t = start_t + dt
  157.     Do While t < stop_t
  158.         pic.Line -(X(t), Y(t))
  159.         t = t + dt
  160.     Loop
  161.     pic.Line -(X(stop_t), Y(stop_t))
  162. End Sub
  163. ' Compute the Hermite curve parameters.
  164. Private Sub GetHermiteValues(ByVal ex1 As Single, ByVal ey1 As Single, ByVal ex2 As Single, ByVal ey2 As Single, ByVal vx1 As Single, ByVal vy1 As Single, ByVal vx2 As Single, ByVal vy2 As Single, ByRef Ax As Single, ByRef Bx As Single, ByRef Cx As Single, ByRef Dx As Single, ByRef Ay As Single, ByRef By As Single, ByRef Cy As Single, ByRef Dy As Single)
  165.     Ax = vx2 + vx1 - 2 * ex2 + 2 * ex1
  166.     Bx = 3 * ex2 - 2 * vx1 - 3 * ex1 - vx2
  167.     Cx = vx1
  168.     Dx = ex1
  169.     Ay = vy2 + vy1 - 2 * ey2 + 2 * ey1
  170.     By = 3 * ey2 - 2 * vy1 - 3 * ey1 - vy2
  171.     Cy = vy1
  172.     Dy = ey1
  173. End Sub
  174. ' The parametric function Y(t).
  175. Private Function Y(t As Single) As Single
  176.     Y = Ay * t ^ 3 + By * t * t + Cy * t + Dy
  177. End Function
  178. ' The parametric function X(t).
  179. Private Function X(t As Single) As Single
  180.     X = Ax * t ^ 3 + Bx * t * t + Cx * t + Dx
  181. End Function
  182. ' Prepare to draw the Hermite curve.
  183. Private Sub DrawHermite()
  184. Dim vx1 As Single
  185. Dim vy1 As Single
  186. Dim vx2 As Single
  187. Dim vy2 As Single
  188. Dim dt As Single
  189. Dim i As Integer
  190.     ' Compute the curve parameters.
  191.     vx1 = CSng(txtVx1.Text)
  192.     vy1 = CSng(txtVy1.Text)
  193.     vx2 = CSng(txtVx2.Text)
  194.     vy2 = CSng(txtVy2.Text)
  195.     GetHermiteValues _
  196.         PtX(1), PtY(1), PtX(2), PtY(2), _
  197.         vx1, vy1, vx2, vy2, _
  198.         Ax, Bx, Cx, Dx, Ay, By, Cy, Dy
  199.     ' Draw the curve.
  200.     dt = CSng(txtDt.Text)
  201.     DrawCurve picCanvas, 0, 1, dt
  202.     If chkControlPoints.Value = vbChecked Then
  203.         ' Draw the control points.
  204.         For i = 1 To NumPts
  205.             picCanvas.Line _
  206.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  207.                 Step(2 * GAP, 2 * GAP), , BF
  208.         Next i
  209.         ' Draw the tangents.
  210.         picCanvas.DrawStyle = vbDot
  211.         picCanvas.Line (PtX(1), PtY(1))- _
  212.             (PtX(1) + vx1 / 5, PtY(1) + vy1 / 5)
  213.         picCanvas.Line (PtX(2), PtY(2))- _
  214.             (PtX(2) + vx2 / 5, PtY(2) + vy2 / 5)
  215.         picCanvas.DrawStyle = vbSolid
  216.     End If
  217. End Sub
  218. ' Select a point and start dragging it.
  219. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  220. Dim i As Integer
  221.     ' Find a close point.
  222.     For i = 1 To NumPts
  223.         If Abs(PtX(i) - X) <= GAP And _
  224.            Abs(PtY(i) - Y) <= GAP Then Exit For
  225.     Next i
  226.     If i > NumPts Then Exit Sub
  227.     Dragging = i
  228.     picCanvas.DrawMode = vbInvert
  229.     PtX(Dragging) = X
  230.     PtY(Dragging) = Y
  231.     picCanvas.Line _
  232.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  233.         Step(2 * GAP, 2 * GAP), , BF
  234. End Sub
  235. ' Continue dragging a point.
  236. Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  237.     If Dragging < 1 Then Exit Sub
  238.     picCanvas.Line _
  239.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  240.         Step(2 * GAP, 2 * GAP), , BF
  241.     PtX(Dragging) = X
  242.     PtY(Dragging) = Y
  243.     picCanvas.Line _
  244.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  245.         Step(2 * GAP, 2 * GAP), , BF
  246. End Sub
  247. ' Finish the drag and redraw the curve.
  248. Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  249.     If Dragging < 1 Then Exit Sub
  250.     picCanvas.DrawMode = vbCopyPen
  251.     PtX(Dragging) = X
  252.     PtY(Dragging) = Y
  253.     Dragging = 0
  254.     DrawHermite
  255. End Sub
  256. Private Sub cmdGo_Click()
  257.     DrawHermite
  258. End Sub
  259. Private Sub chkControlPoints_Click()
  260.     DrawHermite
  261. End Sub
  262. Private Sub Form_Load()
  263.     PtX(1) = 0.5 * picCanvas.ScaleWidth
  264.     PtX(2) = 0.8 * picCanvas.ScaleWidth
  265.     PtY(1) = 0.7 * picCanvas.ScaleHeight
  266.     PtY(2) = 0.5 * picCanvas.ScaleHeight
  267. End Sub
  268. ' Make the picCanvas as big as possible.
  269. Private Sub Form_Resize()
  270.     picCanvas.Move 0, picCanvas.Top, _
  271.         ScaleWidth, ScaleHeight - picCanvas.Top
  272.     DrawHermite
  273. End Sub
  274.